package com.example.controller;

import com.example.pojo.Boat;
import com.example.pojo.Query;
import com.example.service.BoatService;
import com.example.utils.Page;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin//解决跨域问题
@RestController//将类标记为控制器
@RequestMapping("/boat")
@Api(tags = "船票管理")
public class BoatController {
    @Autowired
    private BoatService boatService;

    @GetMapping("/selectAllBoat")
    public Result selectAllBoat(@RequestParam(value = "beginningName",defaultValue = "")String beginningName,
                                @RequestParam(value = "destinationName",defaultValue = "")String destinationName,
                                @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                @RequestParam("pageSize")Integer pageSize){
        System.out.println("beginningName:"+beginningName);
        System.out.println("destinationName:"+destinationName);
        Page page=new Page<>(pageNum,pageSize,boatService.count(beginningName,destinationName));
        page.setList(boatService.selectAllBoat(beginningName,destinationName,page.getStart(),pageSize));
        Map<String,Object> map=new HashMap<>();
        map.put("list",page.getList());
        map.put("count",page.getTotalCount());//总记录数
        map.put("pageSize", pageSize);//每页记录数
        map.put("pageNum", pageNum);//当前页码
        return new Result<>("200","查询成功",map);
    }

    @PostMapping("/addBoat")//添加飞机
    public Result addBoat(@RequestBody Boat boat){
        int count=boatService.addBoat(boat);
        Map<String,Object> map=new HashMap<>();
        if(count>0){
            map.put("count",200);
            map.put("data",boat);
            map.put("msg","添加成功");
        }else {
            map.put("count",444);
            map.put("data",null);
            map.put("msg","添加失败");
        }
        return new Result<>("222","调用成功",map);
    }

    @GetMapping("/deleteBoat")//删除飞机
    public Result deleteBoat(@RequestParam("id") Integer id){
        int count=boatService.deleteBoat(id);
        Map<String,Object> map=new HashMap<>();
        if(count>0){
            map.put("count",200);
            map.put("data",null);
            map.put("msg","删除成功");
        }else {
            map.put("count",444);
            map.put("data",null);
            map.put("msg","删除失败");
        }
        return new Result<>("222","调用成功",map);
    }

    @PostMapping("/updateBoat")//修改飞机
    public Result updateBoat(@RequestBody Boat boat){
        int count=boatService.updateBoat(boat);
        Map<String,Object> map=new HashMap<>();
        if(count>0){
            map.put("count",200);
            map.put("data",boat);
            map.put("msg","更新成功");
        }else {
            map.put("count",444);
            map.put("data",null);
            map.put("msg","更新失败");
        }
        return new Result<>("222","调用成功",map);
    }

    @GetMapping("/selectByIdBoat")//根据id查询飞机
    public Result selectByIdBoat(@RequestParam("id") Integer id){
        Boat boat=boatService.selectByIdBoat(id);
        Map<String,Object> map=new HashMap<>();
        if(boat!=null){
            map.put("code",200);
            map.put("data",boat);
            map.put("msg","查询成功");
        }else {
            map.put("code",444);
            map.put("data",null);
            map.put("msg","查询失败");
        }
        return new Result<>("222","调用成功",map);
    }
}
